using System;
using System.Collections.Generic;
using UnityEngine;

public static class TeamExtensions
{
	public static int SortPhotonPlayersForTDM(PhotonPlayer a, PhotonPlayer b)
	{
		if (a.GetScore() != b.GetScore())
		{
			return a.GetScore().CompareTo(b.GetScore()) * -1;
		}
		if (PhotonNetwork.player == a)
		{
			return -1;
		}
		if (PhotonNetwork.player == b)
		{
			return 1;
		}
		return string.Compare(a.GetValueByKey(PlayerPropsKeys.nickname, string.Empty), b.GetValueByKey(PlayerPropsKeys.nickname, string.Empty));
	}

	public static int SortPlayersByPoints(PlayerBehavior a, PlayerBehavior b)
	{
		if (a.Points != b.Points)
		{
			return a.Points.CompareTo(b.Points) * -1;
		}
		if (PlayerBehavior.MyPlayer == a)
		{
			return -1;
		}
		if (PlayerBehavior.MyPlayer == b)
		{
			return 1;
		}
		return string.Compare(a.photonView.owner.GetValueByKey(PlayerPropsKeys.nickname, string.Empty), b.photonView.owner.GetValueByKey(PlayerPropsKeys.nickname, string.Empty));
	}

	public static int SortPhotonPlayerForCaptureTheFlag(PhotonPlayer a, PhotonPlayer b)
	{
		if (BCWFlagManager.instance != null)
		{
			float num = 0f;
			float num2 = 0f;
			num = BCWFlagManager.instance.GetPlayerScore(a.ID);
			num2 = BCWFlagManager.instance.GetPlayerScore(b.ID);
			if (num < num2)
			{
				return 1;
			}
			if (num2 < num)
			{
				return -1;
			}
			return SortPhotonPlayersForTDM(a, b);
		}
		return SortPhotonPlayersForTDM(a, b);
	}

	public static void BubbleSort<T>(List<T> list, Comparison<T> comp)
	{
		T val = default(T);
		for (int i = 0; i < list.Count; i++)
		{
			for (int j = 0; j < list.Count - 1; j++)
			{
				if (comp(list[j], list[j + 1]) == 1)
				{
					val = list[j + 1];
					list[j + 1] = list[j];
					list[j] = val;
				}
			}
		}
	}

	public static List<PhotonPlayer> SortedTeam(this PhotonPlayer player)
	{
		List<PhotonPlayer> list = PunTeams.PlayersPerTeam[player.GetTeam()];
		BubbleSort(list, SortPhotonPlayerForCaptureTheFlag);
		return list;
	}

	public static PunTeams.Team GetTeam(this PhotonPlayer player)
	{
		object value;
		if (player != null && player.customProperties.TryGetValue("team", out value))
		{
			return (PunTeams.Team)(byte)value;
		}
		return PunTeams.Team.none;
	}

	public static void SetTeam(this PhotonPlayer player, PunTeams.Team team)
	{
		if (!PhotonNetwork.connectedAndReady)
		{
			Debug.LogWarning(string.Concat("JoinTeam was called in state: ", PhotonNetwork.connectionStateDetailed, ". Not connectedAndReady."));
		}
	}

	public static bool IsTeammate(this PhotonPlayer player, PhotonPlayer mate)
	{
		if (player.GetTeam() == PunTeams.Team.none || mate.GetTeam() == PunTeams.Team.none)
		{
			return false;
		}
		return player.GetTeam() == mate.GetTeam();
	}
}
